package com.zjq.designpatterns.behaviors.strategy.code;/*
 * 版权信息: 北京千⾥马网信科技有限公司</br>
 * Copyright ©2008-2018. All rights reserved. 京ICP备16007318号
 */

import java.util.HashMap;
import java.util.Map;

/**
 * @author jessie
 * @version 1.0
 * @date 2021/1/25
 */
public class PromotionStrategyFactory {
    private static Map<String, PromotionStrategy> PROMOTION_STRATEGY_MAP = new HashMap<>();

    static {
        PROMOTION_STRATEGY_MAP.put(PromotionKey.LIJIAN, new LijianPromotionStrategy());
        PROMOTION_STRATEGY_MAP.put(PromotionKey.FANXIAN, new FanxianPromotionStrategy());
        PROMOTION_STRATEGY_MAP.put(PromotionKey.MANJIAN, new ManjianPromotionStrategy());
    }

    private static final PromotionStrategy NON_PROMOTION = new EmptyPromotionStrategy();

    private PromotionStrategyFactory() {

    }

    public static PromotionStrategy getPromotionStrategy(String promotionKey) {
        PromotionStrategy promotionStrategy = PROMOTION_STRATEGY_MAP.get(promotionKey);
        //防止空指针，我们这里创建一个空策略
        return promotionStrategy == null ? NON_PROMOTION : promotionStrategy;
    }

    //在声明常量的时候，起到一个逻辑上分组的作用， 这里可以加final，不过是置灰的，意思是不可更改的
    private interface PromotionKey{
        String LIJIAN = "LIJIAN";
        String FANXIAN = "FANXIAN";
        String MANJIAN = "MANJIAN";
    }
}
